home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap14 / Blowup / Blowup.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  8.4 KB  |  271 lines

  1. /*---------------------------------------
  2.    BLOWUP.C -- Video Magnifier Program
  3.                (c) Charles Petzold, 1998
  4.   ---------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <stdlib.h>      // for abs definition
  8. #include "resource.h"
  9.  
  10. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  11.  
  12. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  13.                     PSTR szCmdLine, int iCmdShow)
  14. {
  15.      static TCHAR szAppName [] = TEXT ("Blowup") ;
  16.      HACCEL       hAccel ;
  17.      HWND         hwnd ;
  18.      MSG          msg ;
  19.      WNDCLASS     wndclass ;
  20.  
  21.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  22.      wndclass.lpfnWndProc   = WndProc ;
  23.      wndclass.cbClsExtra    = 0 ;
  24.      wndclass.cbWndExtra    = 0 ;
  25.      wndclass.hInstance     = hInstance ;
  26.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  27.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  28.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  29.      wndclass.lpszMenuName  = szAppName ;
  30.      wndclass.lpszClassName = szAppName ;
  31.      
  32.      if (!RegisterClass (&wndclass))
  33.      {
  34.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  35.                       szAppName, MB_ICONERROR) ;
  36.           return 0 ;
  37.      }
  38.      
  39.      hwnd = CreateWindow (szAppName, TEXT ("Blow-Up Mouse Demo"), 
  40.                           WS_OVERLAPPEDWINDOW, 
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           CW_USEDEFAULT, CW_USEDEFAULT,
  43.                           NULL, NULL, hInstance, NULL) ;
  44.  
  45.      ShowWindow (hwnd, iCmdShow) ;
  46.      UpdateWindow (hwnd) ;
  47.  
  48.      hAccel = LoadAccelerators (hInstance, szAppName) ;
  49.  
  50.      while (GetMessage (&msg, NULL, 0, 0))
  51.      {
  52.           if (!TranslateAccelerator (hwnd, hAccel, &msg))
  53.           {
  54.                TranslateMessage (&msg) ;
  55.                DispatchMessage (&msg) ;
  56.           }
  57.      }
  58.      return msg.wParam ;
  59. }
  60.  
  61. void InvertBlock (HWND hwndScr, HWND hwnd, POINT ptBeg, POINT ptEnd)
  62. {
  63.      HDC hdc ;
  64.  
  65.      hdc = GetDCEx (hwndScr, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE) ;
  66.      ClientToScreen (hwnd, &ptBeg) ;
  67.      ClientToScreen (hwnd, &ptEnd) ;
  68.      PatBlt (hdc, ptBeg.x, ptBeg.y, ptEnd.x - ptBeg.x, ptEnd.y - ptBeg.y,
  69.              DSTINVERT) ;
  70.      ReleaseDC (hwndScr, hdc) ;
  71. }
  72.  
  73. HBITMAP CopyBitmap (HBITMAP hBitmapSrc)
  74. {
  75.      BITMAP  bitmap ;
  76.      HBITMAP hBitmapDst ;
  77.      HDC     hdcSrc, hdcDst ;
  78.  
  79.      GetObject (hBitmapSrc, sizeof (BITMAP), &bitmap) ;
  80.      hBitmapDst = CreateBitmapIndirect (&bitmap) ;
  81.  
  82.      hdcSrc = CreateCompatibleDC (NULL) ;
  83.      hdcDst = CreateCompatibleDC (NULL) ;
  84.  
  85.      SelectObject (hdcSrc, hBitmapSrc) ;
  86.      SelectObject (hdcDst, hBitmapDst) ;
  87.  
  88.      BitBlt (hdcDst, 0, 0, bitmap.bmWidth, bitmap.bmHeight,
  89.              hdcSrc, 0, 0, SRCCOPY) ;
  90.  
  91.      DeleteDC (hdcSrc) ;
  92.      DeleteDC (hdcDst) ;
  93.  
  94.      return hBitmapDst ;
  95. }
  96.  
  97. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  98. {
  99.      static BOOL    bCapturing, bBlocking ;
  100.      static HBITMAP hBitmap ;
  101.      static HWND    hwndScr ;
  102.      static POINT   ptBeg, ptEnd ;
  103.      BITMAP         bm ;
  104.      HBITMAP        hBitmapClip ;
  105.      HDC            hdc, hdcMem ;
  106.      int            iEnable ;
  107.      PAINTSTRUCT    ps ;
  108.      RECT           rect ;
  109.  
  110.      switch (message)
  111.      {
  112.      case WM_LBUTTONDOWN:
  113.           if (!bCapturing)
  114.           {
  115.                if (LockWindowUpdate (hwndScr = GetDesktopWindow ()))
  116.                {
  117.                     bCapturing = TRUE ;
  118.                     SetCapture (hwnd) ;
  119.                     SetCursor (LoadCursor (NULL, IDC_CROSS)) ;
  120.                }
  121.                else
  122.                     MessageBeep (0) ;
  123.           }
  124.           return 0 ;
  125.  
  126.      case WM_RBUTTONDOWN:
  127.           if (bCapturing)
  128.           {
  129.                bBlocking = TRUE ;
  130.                ptBeg.x = LOWORD (lParam) ;
  131.                ptBeg.y = HIWORD (lParam) ;
  132.                ptEnd = ptBeg ;
  133.                InvertBlock (hwndScr, hwnd, ptBeg, ptEnd) ;
  134.           }
  135.           return 0 ;
  136.  
  137.      case WM_MOUSEMOVE:
  138.           if (bBlocking)
  139.           {
  140.                InvertBlock (hwndScr, hwnd, ptBeg, ptEnd) ;
  141.                ptEnd.x = LOWORD (lParam) ;
  142.                ptEnd.y = HIWORD (lParam) ;
  143.                InvertBlock (hwndScr, hwnd, ptBeg, ptEnd) ;
  144.           }
  145.           return 0 ;
  146.  
  147.      case WM_LBUTTONUP:
  148.      case WM_RBUTTONUP:
  149.           if (bBlocking)
  150.           {
  151.                InvertBlock (hwndScr, hwnd, ptBeg, ptEnd) ;
  152.                ptEnd.x = LOWORD (lParam) ;
  153.                ptEnd.y = HIWORD (lParam) ;
  154.  
  155.                if (hBitmap)
  156.                {
  157.                     DeleteObject (hBitmap) ;
  158.                     hBitmap = NULL ;
  159.                }
  160.  
  161.                hdc = GetDC (hwnd) ;
  162.                hdcMem = CreateCompatibleDC (hdc) ;
  163.                hBitmap = CreateCompatibleBitmap (hdc, 
  164.                                    abs (ptEnd.x - ptBeg.x),
  165.                                    abs (ptEnd.y - ptBeg.y)) ;
  166.  
  167.                SelectObject (hdcMem, hBitmap) ;
  168.  
  169.                StretchBlt (hdcMem, 0, 0, abs (ptEnd.x - ptBeg.x),
  170.                                          abs (ptEnd.y - ptBeg.y), 
  171.                            hdc, ptBeg.x, ptBeg.y, ptEnd.x - ptBeg.x, 
  172.                                                   ptEnd.y - ptBeg.y, SRCCOPY) ;
  173.  
  174.                DeleteDC (hdcMem) ;
  175.                ReleaseDC (hwnd, hdc) ;
  176.                InvalidateRect (hwnd, NULL, TRUE) ;
  177.           }
  178.           if (bBlocking || bCapturing)
  179.           {
  180.                bBlocking = bCapturing = FALSE ;
  181.                SetCursor (LoadCursor (NULL, IDC_ARROW)) ;
  182.                ReleaseCapture () ;
  183.                LockWindowUpdate (NULL) ;
  184.           }
  185.           return 0 ;
  186.  
  187.      case WM_INITMENUPOPUP:
  188.           iEnable = IsClipboardFormatAvailable (CF_BITMAP) ? 
  189.                               MF_ENABLED : MF_GRAYED ;
  190.  
  191.           EnableMenuItem ((HMENU) wParam, IDM_EDIT_PASTE, iEnable) ;
  192.  
  193.           iEnable = hBitmap ? MF_ENABLED : MF_GRAYED ;
  194.  
  195.           EnableMenuItem ((HMENU) wParam, IDM_EDIT_CUT,    iEnable) ;
  196.           EnableMenuItem ((HMENU) wParam, IDM_EDIT_COPY,   iEnable) ;
  197.           EnableMenuItem ((HMENU) wParam, IDM_EDIT_DELETE, iEnable) ;
  198.           return 0 ;
  199.  
  200.      case WM_COMMAND:
  201.           switch (LOWORD (wParam))
  202.           {
  203.           case IDM_EDIT_CUT:
  204.           case IDM_EDIT_COPY:
  205.                if (hBitmap)
  206.                {
  207.                     hBitmapClip = CopyBitmap (hBitmap) ;
  208.                     OpenClipboard (hwnd) ;
  209.                     EmptyClipboard () ;
  210.                     SetClipboardData (CF_BITMAP, hBitmapClip) ;
  211.                }
  212.                if (LOWORD (wParam) == IDM_EDIT_COPY)
  213.                     return 0 ;
  214.                                         // fall through for IDM_EDIT_CUT
  215.           case IDM_EDIT_DELETE:
  216.                if (hBitmap)
  217.                {
  218.                     DeleteObject (hBitmap) ;
  219.                     hBitmap = NULL ;
  220.                }
  221.                InvalidateRect (hwnd, NULL, TRUE) ;
  222.                return 0 ;
  223.  
  224.           case IDM_EDIT_PASTE:
  225.                if (hBitmap)
  226.                {
  227.                     DeleteObject (hBitmap) ;
  228.                     hBitmap = NULL ;
  229.                }
  230.                OpenClipboard (hwnd) ;
  231.                hBitmapClip = GetClipboardData (CF_BITMAP) ;
  232.  
  233.                if (hBitmapClip)
  234.                     hBitmap = CopyBitmap (hBitmapClip) ;
  235.  
  236.                CloseClipboard () ;
  237.                InvalidateRect (hwnd, NULL, TRUE) ;
  238.                return 0 ;
  239.           }
  240.           break ;
  241.  
  242.      case WM_PAINT:
  243.           hdc = BeginPaint (hwnd, &ps) ;
  244.  
  245.           if (hBitmap)
  246.           {
  247.                GetClientRect (hwnd, &rect) ;
  248.  
  249.                hdcMem = CreateCompatibleDC (hdc) ;
  250.                SelectObject (hdcMem, hBitmap) ;
  251.                GetObject (hBitmap, sizeof (BITMAP), (PSTR) &bm) ;
  252.                SetStretchBltMode (hdc, COLORONCOLOR) ;
  253.  
  254.                StretchBlt (hdc,    0, 0, rect.right, rect.bottom,
  255.                            hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY) ;
  256.  
  257.                DeleteDC (hdcMem) ;
  258.           }
  259.           EndPaint (hwnd, &ps) ;
  260.           return 0 ;
  261.  
  262.      case WM_DESTROY:
  263.           if (hBitmap)
  264.                DeleteObject (hBitmap) ;
  265.  
  266.           PostQuitMessage (0) ;
  267.           return 0 ;
  268.      }
  269.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  270. }
  271.